package com.lazy.mongo.ex;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

import org.bson.Document;
import org.bson.types.ObjectId;
import org.junit.Test;

import com.mongodb.MongoClient;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.gridfs.GridFSBucket;
import com.mongodb.client.gridfs.GridFSBuckets;
import com.mongodb.client.gridfs.GridFSUploadStream;
import com.mongodb.client.gridfs.model.GridFSUploadOptions;

public class GridFsDemo {

    @Test
    public void testOne() {
        String host = "192.168.1.234";
        int port = 27017;
        MongoClient mongoClient =  new MongoClient(host, port);
        // get handle to "mydb" database
        MongoDatabase database = mongoClient.getDatabase("mydb");
        database.drop();

        GridFSBucket gridFSBucket = GridFSBuckets.create(database);
        
        /*
         * UploadFromStream Example
         */
        // Get the input stream
        InputStream streamToUploadFrom = new ByteArrayInputStream("Hello World".getBytes(StandardCharsets.UTF_8));

        // Create some custom options
        GridFSUploadOptions options = new GridFSUploadOptions()
                .chunkSizeBytes(1024)
                .metadata(new Document("type", "presentation"));

        ObjectId fileId = gridFSBucket.uploadFromStream("mongodb-tutorial", streamToUploadFrom, options);
        try{
            streamToUploadFrom.close();
        }catch(IOException e){
            e.printStackTrace();
        }
        System.out.println("The fileId of the uploaded file is: " + fileId.toHexString());
    }
    
    @Test
    public void testMulti() {
        // String host = "192.168.1.234";
        String host = "192.168.1.178";
        int port = 27017;
        MongoClient mongoClient =  new MongoClient(host, port);
        // get handle to "mydb" database
        MongoDatabase database = mongoClient.getDatabase("mydb");
        database.drop();

        GridFSBucket gridFSBucket = GridFSBuckets.create(database);
        
        //long start = System.currentTimeMillis();
        /*
         * UploadFromStream Example
         */
        String data = "database mydb on connection [connectionId{localValue :2, serverValue:57}] to server 127.0.0.1:27017 ";
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 10000; i++) {
            sb.append(data);
        }
        long start = System.currentTimeMillis();
        byte[] binData = sb.toString().getBytes(StandardCharsets.UTF_8);
        // Create some custom options
        GridFSUploadOptions options = new GridFSUploadOptions()
                .chunkSizeBytes(4 * 1024000)
                .metadata(new Document("type", "presentation"));
        
        for (int j = 0; j < 1000; j++) {
            // Get the input stream
            InputStream streamToUploadFrom = new ByteArrayInputStream(binData);
    
            ObjectId fileId = gridFSBucket.uploadFromStream("mongodb-tutorial" + j, streamToUploadFrom, options);
            try{
                streamToUploadFrom.close();
            }catch(IOException e){
                e.printStackTrace();
            }
        }
        //System.out.println("The fileId of the uploaded file is: " + fileId.toHexString());
        System.out.println("took times " + (System.currentTimeMillis() - start));
    }
    
    @Test
    public void testMulti2() {
        //String host = "192.168.1.234";
        //int port = 27027;
        String host = "192.168.1.178";
        int port = 27017;
        MongoClient mongoClient =  new MongoClient(host, port);
        // get handle to "mydb" database
        MongoDatabase database = mongoClient.getDatabase("mydb");
        database.drop();

        GridFSBucket gridFSBucket = GridFSBuckets.create(database);
        
        //long start = System.currentTimeMillis();
        /*
         * UploadFromStream Example
         */
        String data = "database mydb on connection [connectionId{localValue :2, serverValue:57}] to server 127.0.0.1:27017 ";
        StringBuilder sb = new StringBuilder();
        // 100 bytes * 10000 = 1M
        for (int i = 0; i < 10000; i++) {
            sb.append(data);
        }
        long start = System.currentTimeMillis();
        byte[] binData = sb.toString().getBytes(StandardCharsets.UTF_8);
        
        // Create some custom options
        //GridFSUploadOptions options = new GridFSUploadOptions()
        //        .chunkSizeBytes(1024000)
        //        .metadata(new Document("type", "presentation"));
        for (int j = 0; j < 1000; j++) {
            GridFSUploadStream uploadStream = gridFSBucket.openUploadStream("mongodb-tutorial" + j);
            uploadStream.write(binData);
            uploadStream.close();
        }
        //System.out.println("The fileId of the uploaded file is: " + fileId.toHexString());
        System.out.println("took times " + (System.currentTimeMillis() - start));
    }
}
